/**
 * 版权所有 2009-2015 中国中医科学院中医药信息研究所 何前锋 frontpeak@yahoo.com.cn
 **/
package cn.ac.cintcm.sh.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.ac.cintcm.sh.domain.PresHerb;

public class PrescriptionHerbDao {
    
    HsqldbDao dao = (new HsqldbDao()).getDao();
    
    public List<PresHerb> getPresHerbs() {
        String sql="select * from HERB_IN_PRESCRIPTION";
        
        dao.openConnection();
        List<PresHerb> ph = new ArrayList<PresHerb>();
        ResultSet rs = dao.executeQuery(sql);
        try{
            while(rs.next()){
                int id=rs.getInt("id");
                int p_id = rs.getInt("PRESCRIPTION_ID");
                float dose = rs.getFloat("DOSAGE");
                String odose = rs.getString("ODOSAGE");
                String unit = rs.getString("unit");
                PresHerb a = new PresHerb();
                a.setId(id);
                a.setPid(p_id);
                a.setDosage(dose);
                a.setOdosage(odose);
                a.setUnit(unit);
                ph.add(a);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        dao.closeConnection();
        return ph;
    }

    public void newPrescriptionHerbRelation(List<PresHerb> phs) {
        String sql = "insert into herb_pres values(";
        
        dao.openConnection();
        for (PresHerb ph : phs) {
            String s = sql + ph.getInsertStr() + ")";
            dao.executeUpdate(s);
        }
        dao.closeConnection();
        
    }
}